//
//  Lock9ViewController.swift
//  SampleApp
//
//  Created by Kevin on 2019/7/2.
//  Copyright © 2019 Kevin. All rights reserved.
//

import UIKit

class Lock9ViewController: VCBase {
    
    // 成员变量
    var isTwo:Bool = true
    var isLogin:Bool = true
    // 临时密码
    private var tempPwd:String? = nil
    
    // 密码输入
    private lazy var password: GesturePasswordView = {
        let ui = GesturePasswordView()
        ui.center = view.center
        ui.lineNormalColor = COLOR_BLUE
        let uiW = SCREEN_WIDTH - MARGIN20
        ui.itemCenterBallColor = COLOR_BLUE
        ui.itemBackGoundColor = COLOR_LIGHT_BLUE
        ui.bounds = CGRect(x: 0, y: 0, width: uiW, height: uiW)
        return ui
    }()
    
    // 取消按钮
    private lazy var cancel: CancelView = {
        let ui = CancelView(frame: CGRect(x: 16, y: realHeight + MARGIN10, width: 50, height: 50))
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 手势提示
    private lazy var waggleView:WaggleView = {
        let ui = WaggleView()
        ui.frame = CGRect(x: 0, y: SCREEN_HEIGHT * 0.17, width: SCREEN_WIDTH, height: MARGIN)
        return ui
    }()
    
    // 回调
    var callback:(_ str:String,_ status:GestureStatus) -> Void = {str,status in}
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_set()
        init_view()
        init_listener()
    }
}

enum GestureStatus {
    case CANCEL
    case SUCCESS
    case DIFFERENT
}

extension Lock9ViewController {
    
    private func init_set() -> Void {
        view.backgroundColor = UIColor.white
    }
    
    private func init_view() -> Void {
        view.addSubview(cancel)
        view.addSubview(password)
        view.addSubview(waggleView)
    }
    
    private func init_listener() -> Void {
        // 取消监听
        cancel.callback = {
            self.callback("",GestureStatus.CANCEL)
            self.exit()
            return
        }
        
        // 手势监听
        password.addPasswordBlock = { (pwd) in
            if pwd.count < 4 {
                self.errorTip(str: "请至少连接4个点")
                return
            }
            // 注册
            if self.isTwo {
                if self.tempPwd != nil{
                    if self.tempPwd!.elementsEqual(pwd){
                        self.callback(pwd,GestureStatus.SUCCESS)
                        self.exit()
                    }else{
                        self.callback(pwd,GestureStatus.DIFFERENT)
                        self.errorTip(str: "您两次输入的手势不一致")
                    }
                }else {
                    self.tempPwd = pwd
                    self.password.refresh()
                    self.errorTip(str: "请再次确认手势密码",isError: false)
                }
            }else{
                // 登录
                if self.isLogin {
                    self.authByGesture(str: SP_GESTURE_LOGIN_PWD, pwd: pwd)
                }
                // 支付
                if !self.isLogin {
                    self.authByGesture(str: SP_GESTURE_PAY_PWD, pwd: pwd)
                }
            }
        }
    }
    
    private func authByGesture(str:String,pwd:String) -> Void {
        if let password = UserDefaults.standard.string(forKey: str) {
            if password.elementsEqual(pwd) {
                self.callback(pwd,GestureStatus.SUCCESS)
                self.exit()
            }else {
                self.errorTip(str: "您输入的手势不正确")
            }
        }
    }
    
    private func errorTip(str:String,isError:Bool = true) -> Void {
        self.waggleView.resetTipText = str
        self.waggleView.shake()
        if isError {
            self.password.updateError()
        }
    }
    
    func exit() -> Void {
        self.password.refresh()
        self.dismiss(animated: true, completion: nil)
    }
}
